package com.ljkj.blockpay.basedata;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ljkj.blockpay.util.StatusBarUtil;
import com.trello.rxlifecycle.components.support.RxFragment;

import org.greenrobot.eventbus.EventBus;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;

/**
 * Created by tianping on 2020/2/3.
 */

public abstract class BaseFragment<V extends ViewDataBinding, P extends BasePresenterImpl> extends RxFragment {
    protected Activity mActivity;
    protected V binding;
    private P mPresenter;//功能调用

    /**
     * 视图是否已经初初始化
     */
    protected boolean isInit = false;
    protected boolean isLoad = false;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (Activity) context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        isInit = true;
        binding = DataBindingUtil.inflate(inflater, initContentView(), container, false);
        compatible();
         StatusBarUtil.setRootViewFitsSystemWindows(getActivity(),true);
        StatusBarUtil.setTranslucentStatus(getActivity());
        if (!StatusBarUtil.setStatusBarDarkTheme(getActivity(),true)){
            StatusBarUtil.setStatusBarColor(getActivity(),0x55000000);
        }

        return binding.getRoot();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setPresenter();
        initView();
        setListener();
        initData();
        isCanLoadData();
    }

    private void isCanLoadData() {
        if (!isInit) {
            return;
        }

//        if (getUserVisibleHint()) {
//            lazyLoad();
//            isLoad = true;
//        } else {
//            if (isLoad) {
//                stopLoad();
//            }
//        }
    }

    /**
     * 获取presenter
     *
     * @return
     */
    protected P getPresenter() {
        return mPresenter;
    }

    protected void setPresenter() {
        if (mPresenter == null)
            mPresenter = createPresenter();
    }

    /**
     * 当视图初始化并且对用户可见的时候去真正的加载数据
     */
    protected void lazyLoad() {
    }

    /**
     * 当视图已经对用户不可见并且加载过数据，如果需要在切换到其他页面时停止加载数据，可以覆写此方法
     */
    protected void stopLoad() {
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        isInit = false;
        isLoad = false;
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        removeEventListener();
    }

    /**
     * Sets layout id.
     *
     * @return the layout id
     */
    protected abstract int initContentView();

    protected abstract P createPresenter();

    /**
     * 初始化数据
     */
    protected void initData() {

    }

    /**
     * view与数据绑定
     */
    protected void initView() {

    }

    /**
     * 设置监听
     */
    protected void setListener() {

    }

    /**
     * 找到activity的控件
     *
     * @param <T> the type parameter
     * @param id  the id
     * @return the t
     */
    @SuppressWarnings("unchecked")
    protected <T extends View> T findActivityViewById(@IdRes int id) {
        return (T) mActivity.findViewById(id);
    }



    /**
     * 注册全局监听事件
     */
    protected void registerEventListener() {
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    /**
     * 注销全局监听事件
     */
    protected void removeEventListener() {
        EventBus.getDefault().unregister(this);
        EventBus.getDefault().removeAllStickyEvents();
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void compatible() {
        //兼容8.1系统输入框获取焦点问题
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
            getActivity().getWindow().getDecorView().setImportantForAutofill(
                    View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
        }
    }
}
